package cn.javabb.web.controller;

import cn.javabb.common.exception.ParameterException;
import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.sys.UserAuthDO;
import cn.javabb.core.entity.sys.UserDO;
import cn.javabb.core.model.StatusEnum;
import cn.javabb.core.query.PostQuery;
import cn.javabb.core.query.CommentQuery;
import cn.javabb.core.service.biz.PostService;
import cn.javabb.core.service.biz.CommentService;
import cn.javabb.core.service.sys.UserAuthService;
import cn.javabb.core.service.sys.UserService;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserAuthService userAuthService;
    @RequiresAuthentication
    @GetMapping("/userConfig")
    public String userInfo(Model model) {
        return "web/view/user/userConfig";
    }

    /**
     * 绑定账号页面
     * @param model
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/oauth")
    public String oauth(Model model) {
        UserDO loginUser = getLoginUser();
        List<UserAuthDO> userAuthList = userAuthService.listUserAuthByUserId(loginUser.getId());
        Map<String, Object> authMap = new HashMap<>();
        userAuthList.forEach(auth -> authMap.put(auth.getAuthType(), auth));
        model.addAttribute("authMap", authMap);
        return "web/view/center/accountOAuth";
    }

    /**
     * 基本信息配置页面
     * @param model
     * @return
     */
    @GetMapping("/config")
    public String config(Model model) {
        return "web/view/user/userConfig";
    }

    /**
     * 发帖列表页面
     * @param model
     * @return
     */
    @GetMapping("/post")
    public String post(Model model) {
        return "web/view/user/userPost";
    }

    /**
     * 评论页面
     * @param model
     * @return
     */
    @GetMapping("/comment")
    public String comment(Model model) {
        return "web/view/user/userComment";
    }
    @GetMapping("/{userId}")
    public String userInfo(@PathVariable Integer userId,Model model) {
        UserDO user = new UserDO();
        user = userService.queryById(userId);
        model.addAttribute("author", user);
        model.addAttribute("authorId", userId);
        //
        return "web/view/user/userInfo";
    }
    @ResponseBody
    @RequestMapping("/queryUsernameRepeat")
    public Object queryUsernameRepeat(@RequestParam String username){
        if(StringUtils.isBlank(username)){
            return ResponseModel.Failure("参数错误",403);
        }
        UserDO user = new UserDO().setUserName(username).setState(StatusEnum.NORMAL.getCode());
        if(userService.queryCountByWhere(user)>0){
            return ResponseModel.Failure();
        }
        return ResponseModel.Success();
    }

    /**
     * 用户帖子
     * @return
     */
    @RequiresAuthentication
    @ResponseBody
    @GetMapping("/comments")
    public Object userPostList(CommentQuery query){
        if (null == query.getUserId()) {
            throw new ParameterException();
        }
        UserDO loginUser = getLoginUser();
        if (!loginUser.getId().equals( query.getUserId())) {
            throw new ParameterException("操作失误");
        }
        Page page = commentService.listPostComments(query);
        return ResponseModel.table(page);
    }

}

